
#include <string.h>
#include <stdio.h>
#include "TargaTexture.h"

unsigned char  TargaTexture::uncompressed[12] = {0,0, 2,0,0,0,0,0,0,0,0,0};
unsigned char  TargaTexture::compressed[12]   = {0,0,10,0,0,0,0,0,0,0,0,0};

// Initializes class
TargaTexture::TargaTexture() { 

  imageData = NULL;
}

// Finalizes class
TargaTexture::~TargaTexture() { 

  if (imageData) {

    delete[] imageData;
    imageData = NULL;
  }
}
/*
void TargaTexture::generate(GLint components) {
	glGenTextures(1,&texID);
	
	glBindTexture(GL_TEXTURE_2D, texID);
    glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

	gluBuild2DMipmaps(GL_TEXTURE_2D,components,width,height,GL_RGBA,GL_UNSIGNED_BYTE,imageData);
}
//*/
/*
void TargaTexture::environment(GLuint envmode,GLuint genmode) {

    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envmode);
    
    switch(genmode) {
        case BSB_TEX_CLAMP:
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE_EXT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE_EXT);
            break;
        case BSB_TEX_SPHERICAL:
	        glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
	        glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
	        glEnable(GL_TEXTURE_GEN_S);
	        glEnable(GL_TEXTURE_GEN_T);
	        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
            break;
    }
}
//*/

/*
void TargaTexture::enable() {
	glBindTexture(GL_TEXTURE_2D, texID);
    glEnable(GL_TEXTURE_2D);
}
//*/
/*
void TargaTexture::release() {
    if(imageData) {
        delete[] imageData;
        imageData = NULL;
    }
}
//*/

bool TargaTexture::load(string filename) {

	FILE * fTGA;
	fTGA = fopen(filename.c_str(), "rb");

	if(fTGA == NULL) {
    cerr << ">> TEXTURE: TARGA: Could not open Targa file: " << filename.c_str() << endl;
		return false;
	}

	if(fread(&typeHeader, sizeof(GLubyte[12]), 1, fTGA) == 0) {
    cerr << ">> TEXTURE: TARGA: Could no read Targa mainHeader: " << filename.c_str() << endl;
		if(fTGA != NULL) {
			fclose(fTGA);
		}
		return false;
	}

	if(memcmp((uncompressed + 1), &(typeHeader[1]), sizeof(GLubyte[11])) == 0) {																		// an Uncompressed TGA image
		cerr << ">> TEXTURE: TARGA: Loading uncompressed Targa file: " << filename.c_str() << endl;
    loadUncompressed(filename, fTGA, typeHeader[0]);
	} else if(memcmp(compressed, &typeHeader, sizeof(GLubyte[12])) == 0) {																		// an RLE compressed TGA image
		cerr << ">> TEXTURE: TARGA: Compressed Targa file, ignoring: " << filename.c_str() << endl;
		//LoadCompressedTGA(texture, filename, fTGA);
	} else {
		cerr << ">> TEXTURE: TARGA: Undefined type, ignoring: " << filename.c_str() << endl;
		fclose(fTGA);
		return false;
	}
	return true;
}

bool TargaTexture::loadUncompressed(string filename,FILE *fTGA, int nOffset) {																			// TGA Loading code nehe.gamedev.net)

	if(fread(mainHeader, sizeof(mainHeader), 1, fTGA) == 0) {										
		cerr << ">> TEXTURE: TARGA: Couldn't read Targa file mainHeader: " << filename.c_str() << endl;
		if(fTGA != NULL) {
			fclose(fTGA);
		}
		return false;
	}	

	width = mainHeader[1] * 256 + mainHeader[0];
	height = mainHeader[3] * 256 + mainHeader[2];
	bpp	= mainHeader[4];

	if((width <= 0) || (height <= 0) || ((bpp != 24) && (bpp !=32))) {
		cerr << ">> TEXTURE: TARGA: Invalid texture information: " << filename.c_str() << endl;
		if(fTGA != NULL) {
			fclose(fTGA);
		}
		return false;
	}

	if(bpp == 24) {
		type = GL_RGB;
	} else {
		type = GL_RGBA;
	}

	bytesPerPixel = (bpp / 8);
	imageSize = (bytesPerPixel * width * height);
	imageData = new unsigned char[imageSize];

	if(imageData == NULL) {
		cerr << ">> TEXTURE: TARGA: Couldn't allocate memory for image: " << filename.c_str() << endl;
		fclose(fTGA);
		return false;
	}

  // move pointer over comments
  fread(imageData, sizeof(unsigned char), nOffset, fTGA);

	if(fread(imageData, sizeof(unsigned char), imageSize, fTGA) != imageSize) {
		cerr << ">> TEXTURE: TARGA: Couldn't read Targa image data: " << filename.c_str() << endl;
		if(imageData != NULL) {

			delete[] imageData;
      imageData = NULL;
		}
		fclose(fTGA);
		return false;
	}
//*
	for(GLuint cswap = 0; cswap < (int)imageSize; cswap += bytesPerPixel) {
		imageData[cswap] ^= imageData[cswap+2] ^= imageData[cswap] ^= imageData[cswap+2];
	}
//*/
	fclose(fTGA);
	return true;
}

